package com.barbeque.mall.controller.admin.goods;

import com.barbeque.mall.anno.ParamValidation;
import com.barbeque.mall.bean.BaseRespVo;
import com.barbeque.mall.bean.BaseRespVoNoData;
import com.barbeque.mall.bean.goods.MarketGoods;
import com.barbeque.mall.bean.goods.MarketGoodsAttribute;
import com.barbeque.mall.bean.goods.bo.GoodsAllinone;
import com.barbeque.mall.bean.goods.bo.GoodsListBO;
import com.barbeque.mall.bean.goods.vo.CatAndBrandVO;
import com.barbeque.mall.bean.goods.vo.DetailGoodsVO;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.exception.ValidationException;
import com.barbeque.mall.service.admin.goods.AdminGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Euphonium
 * @since 2022/07/18
 */
@RestController
@RequestMapping("admin/goods")
public class AdminGoodsController {

    @Autowired
    AdminGoodsService adminGoodsService;

    /**
     * admin/goods/list 商品列表-查找(商品id/商品编号/商品名称)
     *
     * @param goodsListBO
     * @return com.barbeque.mall.bean.BaseRespVo
     * @author Euphonium
     * @since 2022/07/18
     */
    @RequiresPermissions("admin:goods:list")
    @GetMapping("list")
    public BaseRespVo listGoods(GoodsListBO goodsListBO) {
        CommonData<MarketGoods> data = adminGoodsService.listGoods(goodsListBO);
        return BaseRespVo.ok(data);
    }

    /**
     * admin/goods/catAndBrand 商品介绍-所属分类/所属品牌商
     *
     * @return com.barbeque.mall.bean.BaseRespVo
     * @author Euphonium
     * @since 2022/07/18
     */
    @GetMapping("catAndBrand")
    public BaseRespVo listCatAndBrand() {
        CatAndBrandVO data = adminGoodsService.listCatAndBrand();
        return BaseRespVo.ok(data);
    }

    /**
     * admin/goods/create 商品上架
     *
     * @param goodsAllinone
     * @param bindingResult
     * @return com.barbeque.mall.bean.BaseRespVoNoData
     * @author Euphonium
     * @since 2022/07/18
     */
    @RequiresPermissions("admin:goods:create")
    @PostMapping("create")
    @ParamValidation
    public BaseRespVoNoData createGoods(@RequestBody @Valid GoodsAllinone goodsAllinone, BindingResult bindingResult) throws ValidationException {
        List<MarketGoodsAttribute> attributes = goodsAllinone.getAttributes();
        if (attributes.size() > 0) {
            for (MarketGoodsAttribute goodsAttribute : attributes) {
                if (StringUtils.isEmpty(goodsAttribute.getAttribute()) || StringUtils.isEmpty(goodsAttribute.getValue())) {
                    throw new ValidationException("参数不对");
                }
            }
        }
        boolean flag = adminGoodsService.createGoods(goodsAllinone);
        if (flag) {
            return BaseRespVoNoData.ok();
        } else {
            return BaseRespVoNoData.error(611, "商品名已经存在");
        }
    }


    /**
     * admin/goods/detail 商品编辑信息显示
     *
     * @param id (商品ID)
     * @return com.barbeque.mall.bean.BaseRespVo
     * @author Euphonium
     * @since 2022/07/19
     */
    @RequiresPermissions("admin:goods:read")
    @GetMapping("detail")
    public BaseRespVo detailGoods(Integer id) {
        DetailGoodsVO data = adminGoodsService.detailGoods(id);
        return BaseRespVo.ok(data);
    }

    /**
     * admin/goods/update 更新商品
     *
     * @param goodsAllinone
     * @param bindingResult
     * @return com.barbeque.mall.bean.BaseRespVoNoData
     * @author Euphonium
     * @since 2022/07/19
     */
    @RequiresPermissions("admin:goods:update")
    @PostMapping("update")
    @ParamValidation
    public BaseRespVoNoData updateGoods(@RequestBody @Valid GoodsAllinone goodsAllinone, BindingResult bindingResult) {
        adminGoodsService.updateGoods(goodsAllinone);
        return BaseRespVoNoData.ok();
    }

    /**
     * admin/goods/delete 删除商品(逻辑删除)
     *
     * @param marketGoods
     * @return com.barbeque.mall.bean.BaseRespVoNoData
     * @author Euphonium
     * @since 2022/07/19
     */
    @RequiresPermissions("admin:goods:delete")
    @PostMapping("delete")
    public BaseRespVoNoData deleteGoods(@RequestBody MarketGoods marketGoods) {
        adminGoodsService.deleteGoods(marketGoods);
        return BaseRespVoNoData.ok();
    }
}
